﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using DevelopStudio.Common.Basic;

namespace DevelopStudio.Basic
{
    public class ChanglistErrorInfo
    {
        private ChanglistErrorInfo()
        {
        }

        public ChanglistErrorInfo(AppEnum.ChangelistErrorCode errorCode, string message)
        {
            this.ErrorCode = errorCode;
            this.Message = message;
        }

        public AppEnum.ChangelistErrorCode ErrorCode { get; set; }

        public string Message { get; set; }

        public override bool Equals(object obj)
        {
            ChanglistErrorInfo temp = obj as ChanglistErrorInfo;

            if (temp == null)
            {
                return false;
            }

            if (this.ErrorCode == temp.ErrorCode &&
                this.Message == temp.Message)
            {
                return true;
            }

            return false;
        }
    }

    [Serializable]
    [XmlRoot("Root")]
    public class ChangelistInfo
    {
        public ChangelistInfo()
        {
            Init();
        }

        private void Init()
        {
            Name = string.Empty;
            LastName = string.Empty;
            BugInfos = new List<BugInfo>();
            this.ErrorInfo = new ChanglistErrorInfo(AppEnum.ChangelistErrorCode.None, string.Empty);
        }

        public event StatusChangedEventHandler StatusChanged;

        public event StatusChangedEventHandler ErrorStatusChanged; 

        private int status;

        [XmlElement("Status")]
        public int Status
        {
            get
            {
                return this.status;
            }

            set
            {
                if (this.status != value)
                {
                    int oldStatus = this.status;
                    this.status = value;

                    if (StatusChanged != null)
                    {
                        StatusChanged(this, new StatusEventArgs(oldStatus, this.status));
                    }
                }
            }
        }

        public void ClearError()
        {
            this.ErrorInfo = new ChanglistErrorInfo(AppEnum.ChangelistErrorCode.None, string.Empty);
        }

        private ChanglistErrorInfo errorInfo;

        [XmlIgnore]
        public ChanglistErrorInfo ErrorInfo
        {
            get
            {
                return this.errorInfo;
            }

            set
            {
                if (this.errorInfo != value)
                {
                    ChanglistErrorInfo oldStatus = this.errorInfo;
                    this.errorInfo = value;

                    if (ErrorStatusChanged != null)
                    {
                        ErrorStatusChanged(this, new StatusEventArgs(oldStatus, this.errorInfo));
                    }
                }
            }
        }

        [XmlElement("Displayed")]
        public bool Displayed { get; set; }

        [XmlElement("WorkingCopyPath")]
        public string WorkingCopyPath { get; set; }

        [XmlElement("Name")]
        public string Name { get; set; }

        [XmlElement("LastName")]
        public string LastName { get; set; }

        [XmlElement("Comment")]
        public string Comment { get; set; }

        [XmlIgnore]
        public string CommitComment
        {
            get
            {
                StringBuilder fixes = new StringBuilder();
                fixes.Append("fixes ");
                StringBuilder commitComment = new StringBuilder();
                commitComment.AppendLine();

                foreach(BugInfo var in this.BugInfos)
                {
                    fixes.Append("#" + var.ID + ",");
                    commitComment.AppendLine(string.Format("#{0} {1}", var.ID, var.Subject));
                }

                commitComment.AppendLine(Comment);

                commitComment.Insert(0, fixes.ToString().TrimEnd(',') + ";");

                return commitComment.ToString();
            }
        }

        [XmlElement("FeedbackComment")]
        public string FeedbackComment { get; set; }

        [XmlElement("StoredPath")]
        public string StoredPath { get; set; }

        [XmlIgnore]
        public MemberInfo SendedBy { get; set; }

        [XmlArray("BugInfos")]
        [XmlArrayItem("BugInfo", typeof(BugInfo))]
        public List<BugInfo> BugInfos { get; set; }

        [XmlIgnore]
        public bool IsResovledBugs { get; set; }
    }
}
